<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>swiper组件封装</title>
    <link rel="stylesheet" href="lib/swiper.css">
    <script src="lib/vue.js"></script>
    <script src="lib/axios.min.js"></script>
    <script src="lib/swiper.js"></script>
    <style>
        img {
            width: 100%;
            height: 300px;
        }
    </style>
</head>

<body>
    <div id="box">
        <!-- 解决方案2-1: -->
        <!-- 利用了vue diff算法更新virtual dom的红利 -->
        <swiper :key="datalist.length">
            <div class="swiper-slide" v-for="data in datalist" :key="data.channelid">
                <img :src="data.thumb">
            </div>
        </swiper>

    </div>

    <script>
        //封装swiper组件
        Vue.component("swiper", {
            template: `<div class="swiper-container">
                <div class="swiper-wrapper">
                    <slot></slot>
                </div>
                <!-- 如果需要分页器 -->
                <div class="swiper-pagination"></div>

                <!-- 如果需要导航按钮 -->
                <div class="swiper-button-prev"></div>
                <div class="swiper-button-next"></div>
            </div>`,
            //解决方案2-2:
            mounted() {
                //swiper
                new Swiper('.swiper-container', {
                    direction: 'horizontal', // 垂直切换选项
                    loop: true, // 循环模式选项

                    // 如果需要分页器
                    pagination: {
                        el: '.swiper-pagination',
                    },

                    // 如果需要前进后退按钮
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                })
            }
        })
        new Vue({
            el: "#box",
            data: {
                datalist: []
            },
            mounted() {
                axios.get("https://api.apiopen.top/musicBroadcasting").then(res => {
                    console.log(res.data.result[0].channellist);
                    this.datalist = res.data.result[0].channellist;
                })
            }
        })
    </script>

</body>

</html>